package cn.edu.nju.askinginhand.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import cn.edu.nju.askinginhand.util.JSONParser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PersonalPasswordActivity extends Activity{
	private EditText oldPasswordEdit;
	private EditText newPasswordEdit;
	private EditText againPasswordEdit;
	private Button   modifyPassword;
	private Button   cancelPassword;
	private String  oldPassword;
	private String   newPassword;
	private String  againPassword;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_password_check);
		 oldPasswordEdit=(EditText) findViewById(R.id.oldPasswordEdit);
		 newPasswordEdit=(EditText) findViewById(R.id.newPasswordEdit);
		 againPasswordEdit=(EditText) findViewById(R.id.againPasswordEdit);
		 modifyPassword=(Button) findViewById(R.id.modifyPassword);
		 cancelPassword=(Button) findViewById(R.id.cancelPassword);
		 initButtons();
	}

	private void initButtons() {
		// TODO Auto-generated method stub
		modifyPassword.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				 oldPassword=oldPasswordEdit.getText().toString();
				 newPassword=newPasswordEdit.getText().toString();
				 againPassword=againPasswordEdit.getText().toString();
				boolean bool=checkInput();
				if(bool){
					if(againPassword.equals(newPassword)){
						if(oldPassword.equals(LoginActivity.personalpassword)){
							 List<NameValuePair> params = new ArrayList<NameValuePair>();
							 params.add(new BasicNameValuePair("uid", LoginActivity.uid));
							 params.add(new BasicNameValuePair("password", newPassword));
							 String raw = JSONParser.getOut("PasswordChangeAction"+LoginActivity.filter,params);
								//Toast.makeText(this, raw, Toast.LENGTH_SHORT).show();		
							 String result = JSONParser.getStringData(raw);
							 if(result.equals("success")){
								Toast.makeText(PersonalPasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
								LoginActivity.personalpassword=newPassword;
								cancelPassword.setText("返回");
							 }else{
								Toast.makeText(PersonalPasswordActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
							 }
							 
						}else{
							Toast.makeText(PersonalPasswordActivity.this, "旧密码错误！", Toast.LENGTH_SHORT).show();
							
						}
						
					}else{
						Toast.makeText(PersonalPasswordActivity.this, "前后密码不一致！", Toast.LENGTH_SHORT).show();
						
					}
				        
				}else{
					
					
				}
			}
			
			
		});
		
		cancelPassword.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
			startActivity(new Intent(PersonalPasswordActivity.this,MainTab.class));
				
			}
			
			
		});
	}
	/*
	 * 
	 * 检查输入是否合法，有待完善。
	 */
	protected boolean checkInput() {
		// TODO Auto-generated method stub
		return true;
	}	
}
